package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcCurTaskQueryMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcCurTaskQuery;
import com.csun.cmny.provider.model.domain.OmcCurTaskQueryGroup;
import com.csun.cmny.provider.model.dto.OmcCurTaskQueryDto;
import com.csun.cmny.provider.model.enums.OmcCurTaskQueryEnum;
import com.csun.cmny.provider.model.vo.query.CurTaskQueryVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcCurTaskQueryGroupService;
import com.csun.cmny.provider.service.OmcCurTaskQueryService;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.PublicUtil;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-10-12 11:33
 * @Description:
 */
@Service
@Transactional
public class OmcCurTaskQueryServiceImpl extends BaseService<OmcCurTaskQuery>
        implements OmcCurTaskQueryService {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcCurTaskQueryMapper omcCurTaskQueryMapper;
    @Resource
    private OmcCurTaskQueryGroupService omcCurTaskQueryGroupService;

    /**
     * add omc cur task query
     *
     * @param omcCurTaskQuery the omc cur task query
     * @return the int.
     */
    @Override
    public Integer add(OmcCurTaskQuery omcCurTaskQuery) {

        // 校验code是否重复
        OmcCurTaskQuery query = new OmcCurTaskQuery();
        query.setCode(omcCurTaskQuery.getCode());
        int count = omcCurTaskQueryMapper.selectCount(query);
        if (count > 0) {
            throw new OmcBizException(ErrorCodeEnum.OMC10031302);
        }

        return omcCurTaskQueryMapper.insertSelective(omcCurTaskQuery);
    }

    /**
     * edit omc cur task query
     *
     * @param omcCurTaskQuery the omc cur task query
     * @return the int.
     */
    @Override
    public Integer edit(OmcCurTaskQuery omcCurTaskQuery) {

        // id 不能为空
        Preconditions.checkArgument(PublicUtil.isNotEmpty(omcCurTaskQuery.getId()),
                cmnyMessageSource.getMessage("cur.task.query.id.null", null));

        // id 是否存在
        OmcCurTaskQuery query = omcCurTaskQueryMapper.selectByPrimaryKey(omcCurTaskQuery.getId());
        if (PublicUtil.isEmpty(query)) {
            throw new OmcBizException(ErrorCodeEnum.OMC10031301);
        }

        // code 是否冲突
        if (PublicUtil.isNotEmpty(omcCurTaskQuery.getCode())) {
            query = new OmcCurTaskQuery();
            query.setCode(omcCurTaskQuery.getCode());
            query = omcCurTaskQueryMapper.selectOne(query);
            if (query != null && !query.getId().equals(omcCurTaskQuery.getId())) {
                throw new OmcBizException(ErrorCodeEnum.OMC10031302);
            }
        }

        return omcCurTaskQueryMapper.updateByPrimaryKeySelective(omcCurTaskQuery);
    }

    /**
     * delete omc cur task query by id
     *
     * @param id the cur task query id
     * @return the int.
     */
    @Override
    public Integer deleteById(Long id) {

        OmcCurTaskQuery query = omcCurTaskQueryMapper.selectByPrimaryKey(id);
        if (PublicUtil.isEmpty(query)) {
            throw new OmcBizException(ErrorCodeEnum.OMC10031301);
        }

        // 查询code是否可以删除
        if (PublicUtil.isNotEmpty(OmcCurTaskQueryEnum.getEnum(query.getCode()))) {
            throw new OmcBizException(ErrorCodeEnum.OMC10031303);
        }

        // 查询id是否在使用中
        OmcCurTaskQueryGroup omcCurTaskQueryGroup = new OmcCurTaskQueryGroup();
        omcCurTaskQueryGroup.setCurTaskQueryId(id);
        int count = omcCurTaskQueryGroupService.selectCount(omcCurTaskQueryGroup);
        if (count > 0) {
            throw new OmcBizException(ErrorCodeEnum.OMC10031304);
        }

        return omcCurTaskQueryMapper.deleteByPrimaryKey(id);
    }

    /**
     * the query cur task query list with page
     *
     * @param omcCurTaskQueryDto the omc cur task query dto
     *
     * @return the list
     */
    @Override
    public List<OmcCurTaskQuery> queryCurTaskQueryListWithPage(OmcCurTaskQueryDto omcCurTaskQueryDto) {

        return omcCurTaskQueryMapper.queryCurTaskQueryListWithPage(omcCurTaskQueryDto);
    }
}
